package com.gzx.spring.drools.controller;

import com.alibaba.fastjson2.JSON;
import com.gzx.risk.core.apis.context.ApiContext;
import com.gzx.risk.core.pojo.CommonResult;
import com.gzx.risk.core.pojo.CreditFile;
import com.gzx.risk.core.pojo.UserInfo;
import com.gzx.risk.core.service.NotifyService;
import com.gzx.risk.core.service.OrderInfoService;
import com.gzx.risk.core.service.RiskService;
import com.gzx.risk.core.tags.CommonLog;
import com.gzx.spring.drools.service.DroolsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "test")
@RestController
@Validated
@RequestMapping("/api/test")
@Slf4j
public class TestController {

    @Resource
    private RiskService riskService;
    @Resource
    private DroolsService droolsService;
    @Resource
    private OrderInfoService orderInfoService;
    @Resource
    private NotifyService notifyService;

    @PostMapping("/findUserInfo")
    public UserInfo check(Long uid){
        return riskService.findUserInfo(uid);
    }

    @Operation(summary = "checkApiRules")
    @CommonLog("checkApiRules")
    @PostMapping("/checkApiRules")
    public CommonResult<String> checkApiRules(@RequestParam(name = "name", defaultValue = "吴岩") String name,
                                              @RequestParam(name = "id", defaultValue = "210727197902096937") String id,
                                              @RequestParam(name = "phone", defaultValue = "13488774341") String phone,
                                              @RequestParam(name = "idLastdate", defaultValue = "2049-12-31") String idLastdate,
                                              @RequestParam(name = "authDate", defaultValue = "20250804-20250830") String authDate,
                                              @RequestParam(name = "lastRefuseDate", defaultValue = "2025-08-01") String lastRefuseDate,
                                              @RequestParam(name = "groupId", defaultValue = "16") Integer groupId)
            throws Exception {
        ApiContext ctx = new ApiContext();
        UserInfo userInfo = new UserInfo();
        userInfo.setName(name);
        userInfo.setId(id);
        userInfo.setPhone(phone);
        userInfo.setIdLastdate(idLastdate);
        ctx.setUserInfo(userInfo);

        ctx.setAuthDate(authDate);
        ctx.setLastRefuseDate(lastRefuseDate);
        droolsService.testExecRules(groupId, ctx);
        return CommonResult.ok("result:" + ctx.getRuleResult());
    }

    @Operation(summary = "mini远程调用合同信息")
    @CommonLog("ContractInfo")
    @PostMapping("/getContractInfo")
    public void getContractInfo(String orderCode) throws Exception {
        CreditFile contractInfo = orderInfoService.getContractInfo(orderCode);
        log.info("contractInfo:{}", JSON.toJSONString(contractInfo));
    };

    @PostMapping("/notifyOrder")
    public void notifyOrder(Long orderId) throws Exception {
        log.info("notifyOrder:{}", JSON.toJSONString(orderId));
        notifyService.notifyOrder(orderId);
    }

}
